package com.heyqing.topic.DistanceBetweenBusStops_1184;

/**
 * ClassName:DistanceBetweenBusStops
 * Package:com.heyqing.topic.DistanceBetweenBusStops_1184
 * Description:
 *
 * @Date:2024/9/16
 * @Author:Heyqing
 */
public class DistanceBetweenBusStops {
    public int distanceBetweenBusStops(int[] distance, int start, int destination) {
        int clockwiseSum = clockwise(distance, start, destination);
        int anticlockwiseSum = anticlockwise(distance, start, destination);
        return Math.min(clockwiseSum, anticlockwiseSum);
    }


    private int clockwise(int[] distance, int start, int destination) {
        int sum = 0;
        for (int i = 0; i < distance.length; i++) {
            if (start == distance.length) start = 0;
            sum += distance[start++];
            if (start == destination || (destination == 0 && start == distance.length)) return sum;
        }
        return sum;
    }

    private int anticlockwise(int[] distance, int start, int destination) {
        int sum = 0;
        for (int i = 0; i < distance.length; i++) {
            if (start == 0) start = distance.length;
            sum += distance[--start];
            if (start == destination) return sum;
        }
        return sum;
    }


    public static void start() {
        DistanceBetweenBusStops distanceBetweenBusStops = new DistanceBetweenBusStops();
        int[] distance = {6,47,48,31,10,27,46,33,52,33,38,25,32,45,36,3,0,33,22,53,8,13,18,1,44,41,14,5,38,25,48};
        int start = 22;
        int destination = 0;
        int res = distanceBetweenBusStops.distanceBetweenBusStops(distance, start, destination);
        System.out.println("res:" + res);
    }
}
